﻿using ClientDao.Corpus;
using ClientInfrastructure.Dto;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using ShareCode.Dto;
using ShareCode.Entity.Corpus;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace ClientAppraise.ViewModels.Pages.Corpus.CorpusType.Controls
{
    public partial class UcCorpusTypeSelectorViewModel(CorpusTypeDal dal) : ObservableObject, IInitDataContextViewModel
    {
        CorpusTypeDal Dal = dal;

        [ObservableProperty]
        private ObservableCollection<BaseTreeDataGridM<CorpusTypeEntity>>? viewData;
        [ObservableProperty]
        private BaseTreeDataGridM<CorpusTypeEntity>? selectedItem;

        PageParameterDto? dataContext;

        public void InitDataContext(PageParameterDto? dataContext)
        {
            this.dataContext = dataContext;
            _ = RefreshData();
        }
        [RelayCommand]
        private void OnSubmit()
        {
            if (SelectedItem == null)
            {
                App.MyDrawer.CloseDrawer(new ResultDto<Object> { Data = new List<CorpusTypeEntity>() });
            }
            else
            {
                List<CorpusTypeEntity> ids = [SelectedItem!.Entity!];
                App.MyDrawer.CloseDrawer(new ResultDto<Object> { Data = ids });
            }
        }
        public async Task RefreshData()
        {
            void fun(List<CorpusTypeEntity>? viewData)
            {
                if (viewData == null) return;

                if (!Application.Current.Dispatcher.CheckAccess())
                {
                    Application.Current.Dispatcher.Invoke(new Action(() => fun(viewData)));
                    return;
                }

                ObservableCollection<BaseTreeDataGridM<CorpusTypeEntity>> _viewData;

                if (dataContext?.Do == PageParameterDtoDo.CheckBox)
                {
                    _viewData = BaseTreeDataGridM<CorpusTypeEntity>.GetList(viewData, null, t => { });

                    if (dataContext.Model is ICollection<long> ids)
                    {
                        BaseTreeDataGridM<CorpusTypeEntity>.CheckCheckBoxItem(_viewData, ids);
                    }
                }
                else
                {
                    //单选
                    _viewData = BaseTreeDataGridM<CorpusTypeEntity>.GetList(viewData);

                    if (dataContext != null && dataContext.Model is List<long> ids)
                    {
                        var list = BaseTreeDataGridM<CorpusTypeEntity>.GetItems(_viewData, ids);

                        if (list != null && list.Count > 0)
                        {
                            SelectedItem = list[0];
                        }
                    }
                }

                BaseTreeDataGridM<CorpusTypeEntity>.ExpandAll(_viewData);

                this.ViewData = _viewData;
            }

            await App.MyContentDialog.ShowRingLoadingAsync(async () =>
            {
                try
                {
                    var data = await Dal.ListAsync<CorpusTypeEntity>(new PagingDto { PageSize = 999999 });

                    if (data.Success)
                    {
                        fun(data.Data);
                    }
                }
                catch (Exception ex)
                {
                    App.MySnackbar.Danger(ex.Message, ex.Message);
                }
                finally
                {
                    App.MyContentDialog.CloseLoading();
                }
            });
        }
    }
}